﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class UpdateAdvImage : Portal.Interface.WebPart
    {
       public override string WebPartId
        {
            get
            {
                return "UpdateAdvImage";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Ảnh quảng cáo");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Description", "");
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(UpdateAdvImage));
            string AdvImageId = System.Web.HttpContext.Current.Request["AdvImageId"];

            PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new AdvImage().PermissionTypes, "AdvImage");
            AdvImageCls AdvImageCls = new AdvImageBLL().CreateModel(Environments.ActiveLanguage, AdvImageId);
            Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UpdateAdvImage", AdvImageId, AdvImageCls.Owner, true);


            string RetHtml =
                Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CMS", "AdvImage.js") +
                
                 Portal.Environment.Environments.GetFormDialog(
                 "<div class=cssFormContent id=divFormContent>" +
                    GetHtml(AdvImageId) +
                 "</div>", ResourceBLL.GetText(WebPartId, "UpdateAdvImageFormTitle", "Cập nhật ảnh quảng cáo"));
                 

            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string GetHtml(string AdvImageId)
        {
            string RetHtml = "";
            AdvImageCls AdvImageCls = new AdvImageBLL().CreateModel(Environments.ActiveLanguage, AdvImageId);
            if (AdvImageCls == null)
            {
                return Environment.Environments.GetRecordNotFound();
            }

            string SelectLanguageText =
             "<select id=divSelectLanguage onchange=javascript:DetectAdvImageDescription('" + AdvImageId + "');>";
            string[] Languages = Environment.Environments.GetLanguages();
            for (int iIndex = 0; iIndex < Languages.Length; iIndex++)
            {
                if (Environment.Environments.ActiveLanguage.Equals(Languages[iIndex]))
                {
                    SelectLanguageText += "<option selected value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
                else
                {
                    SelectLanguageText += "<option value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
            }
            SelectLanguageText += "</select>";
            RetHtml +=
                             "      <table>" +
                              "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "AddNewsLanguage", "Ngôn ngữ") + "</td>" +
                             "              <td>" + SelectLanguageText + "</td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "UpdateAdvImageCaption", "Ảnh quảng cáo") + "</td>" +
                             "              <td><input id=txtAdvImage class=cssTextBoxDialog value=\"" + AdvImageCls.Subject + "\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "UpdateAdvImageDescription", "Mô tả album ảnh") + "</td>" +
                             "              <td><textarea id=txtDescription class=cssTextBoxDialog>" + AdvImageCls.Description + "</textarea></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td></td>" +
                             "              <td><input type=button onclick=javascript:UploadNewImage('" + AdvImageId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "UploadNewImage", "Upload ảnh mới") + "\"><input type=button class=cssSaveButton onclick=javascript:ClientSaveUpdateAdvImage('" + AdvImageId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "UpdateAdvImageSaveButton", "Ghi lại") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "UpdateAdvImageCancelButton", "Thoát") + "\"></td>" +
                             "          </tr>" +
                             "      </table>";



            //                 "<div class=cssAdvImageItems>";
            //                 //"<div><input type=button onclick=\"javascript:ShowFormAppendLink('" + AdvImageId + "');\" value=\"" + ResourceBLL.GetText(WebPartId,"AppendText","Thêm chi tiết") + "\"></div>";//"<a href=\"javascript:AddLinkItem();\">" + Resource.AddLinkItemCaption + "</a></div>";

            //RetHtml += "      <div class=cssAdvImageItems>" +
            //                 "<table>" +
            //                 "<tr>" +
            //                 "      <th></th>"+
            //                 "      <th>" + ResourceBLL.GetText(WebPartId,"LinkItemName","Tên đối tượng") + "</th>" +
            //                 "      <th>" + ResourceBLL.GetText(WebPartId,"LinkItemUrl","Liên kết") + "</th>" +
            //                 "      <th>" + ResourceBLL.GetText(WebPartId,"LinkItemTarget","Target") + "</th>" +
            //                 "      <th>" + ResourceBLL.GetText(WebPartId, "LinkItemDescription", "Mô tả") + "</th>" +
            //                 "      <th>" + ResourceBLL.GetText(WebPartId,"LinkItemIndexOrder","Vị trí") + "</th>" +
            //                 "      <th></th>" +
            //                 "</tr>";
            //for (int iIndex = 0; iIndex < dtAdvImageItems.Rows.Count; iIndex++)
            //{
            //    AdvImageItemCls AdvImageItemCls = new AdvImageItemCls();
            //    AdvImageItemCls.AdvImageItemId = (string)dtAdvImageItems.Rows[iIndex]["AdvImageItemId"];
            //    AdvImageItemCls.AdvImageItemName = (string)dtAdvImageItems.Rows[iIndex]["AdvImageItemName"];
            //    AdvImageItemCls.Url = (string)dtAdvImageItems.Rows[iIndex]["Url"];
            //    try
            //    {
            //        AdvImageItemCls.Description = (string)dtAdvImageItems.Rows[iIndex]["Description"];
            //    }
            //    catch { }
            //    AdvImageItemCls.Target = (string)dtAdvImageItems.Rows[iIndex]["Target"];
            //    AdvImageItemCls.IndexOrder = (int)dtAdvImageItems.Rows[iIndex]["IndexOrder"];
            //    RetHtml += ServerSideNewLinkItem(iIndex,AdvImageId, AdvImageItemCls);
            //}
            //RetHtml += "</table>" +
            //         "</div>";
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideNewLinkItem(int Index, string AdvImageId,AdvImageItemCls AdvImageItemCls)
        {
            string
            Html = "<tr>" +
                   "    <td class=cssUpdateAdvImageCol1><input type=hidden value=\"" + AdvImageItemCls.AdvImageItemId + "\" id=\"txtItemId_" + Index + "\"></td>\r\n" +
                   "    <td class=cssUpdateAdvImageCol1><input class=cssInputItemName value=\"" + AdvImageItemCls.AdvImageItemName + "\" id=\"txtItemName_" + Index + "\"></td>\r\n" +
                   "    <td class=cssUpdateAdvImageCol2><input class=cssInputItemUrl value=\"" + AdvImageItemCls.Url + "\" id=\"txtItemUrl_" + Index + "\"></td>\r\n" +
                   "    <td class=cssUpdateAdvImageCol3><input value=\"" + AdvImageItemCls.Target + "\" class=cssInputItemTarget id=\"txtItemTarget_" + Index + "\"></td>\r\n" +
                   "    <td class=cssUpdateAdvImageCol4><input value=\"" + AdvImageItemCls.Description + "\" class=cssInputItemDescription id=\"txtItemDescription_" + Index + "\"></td>\r\n" +
                   "    <td class=cssUpdateAdvImageCol5><input value=\"" + AdvImageItemCls.IndexOrder.ToString() + "\" class=cssInputItemIndexOrder value=\"" + (Index + 1) + "\" id=\"txtItemIndexOrder_" + Index + "\"></td>\r\n" +
                   "    <td class=cssUpdateAdvImageCol6><input onclick=javascript:DeleteAdvImageItem('" +AdvImageId+"','"+  AdvImageItemCls.AdvImageItemId + "'); type=button value=\"" + ResourceBLL.GetText(WebPartId, "DeleteButton", "Xoá") + "\"></td>\r\n" +
                   "</tr>";
            return Html;
        }


        [AjaxPro.AjaxMethod]
        public AdvImageItemCls[] GetAdvImageItemsObject(int MaxItems)
        {
            AdvImageItemCls[] AdvImageItems = new AdvImageItemCls[MaxItems];
            for (int iIndex = 0; iIndex < AdvImageItems.Length; iIndex++)
            {
                AdvImageItems[iIndex] = new AdvImageItemCls();
            }
            return AdvImageItems;
        }

        [AjaxPro.AjaxMethod]
        public string GetShowAppendForm(string AdvImageId)
        {
            string AppendUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new AddAdvImage().WebPartId),
                        new ParamCls("AdvImageId",AdvImageId)
                    });
            return AppendUrl;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideUpdateAdvImage(string ActiveLanguage,string AdvImageId, string AdvImageName,string Description)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                if (AdvImageName.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId,"EnterName","Nhập tên đối tượng"));
                new AdvImageBLL().UpdateAdvImage(null, ActiveLanguage, AdvImageId, AdvImageName, Description);
                return ResourceBLL.GetText(WebPartId,"MessageUpdateOk","Cập nhật thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerGetDescription(string ActiveLanguage, string AdvImageId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                AdvImageCls AdvImageCls = new AdvImageBLL().CreateModel(ActiveLanguage,AdvImageId);
                return AdvImageCls.Description;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

      [AjaxPro.AjaxMethod]
      public string GetConfirmDeleteMsg()
      {
          return ResourceBLL.GetText(WebPartId, "ConfirmDeleteMsg", "Bạn đã chắc chắn xóa chưa?");
      }


      [AjaxPro.AjaxMethod]
      public string ServerSideDeleteAdvImageItem(string AdvImageItemId)
      {
          try
          {
              Environment.Environments.CheckSessionTimeOut(true);
              new AdvImageBLL().DeleteAdvImageItem(null, AdvImageItemId);
              return ResourceBLL.GetText(WebPartId, "DeleteOk", "Xóa thành công!");
          }
          catch (Exception ex)
          {
              return "ERROR: " + ex.Message.ToString();
          }
      }


      [AjaxPro.AjaxMethod]
      public string GetAddAdvImageItemUrl(string AdvImageId)
      {
          string AppendUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new AdvUploadImage().WebPartId),
                        new ParamCls("AdvImageId",AdvImageId)
                    });
          return AppendUrl;
      }
      
    }
}